package com.ht.system.component;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.ht.common.core.annotation.EnumDictType;
import com.ht.common.core.utils.ClassUtil;
import com.ht.common.core.utils.SnowflakeIdWorker;
import com.ht.common.core.utils.StringUtils;
import com.ht.system.api.dto.DictDataDto;
import com.ht.system.api.dto.DictDto;
import com.ht.system.domain.SysDictData;
import com.ht.system.domain.SysDictType;
import com.ht.system.service.ISysDictDataService;
import com.ht.system.service.ISysDictTypeService;

import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.util.EnumUtil;
import cn.hutool.core.util.ReflectUtil;

/**
 * @Description
 * @Version 1.0
 * @Author zmx
 * @Date 2024/11/13 0013 9:55
 */
@Component
public class InitDictCommonComponent {
    @Autowired
    private ISysDictTypeService dictTypeService;
    @Autowired
    private ISysDictDataService dictDataService;

    

    @Async
	public void initDict() {
        List<Class> cls = ClassUtil.getAllClassByAnnotation(EnumDictType.class, "com.ht.common.core.enums");
        if (StringUtils.isNotEmpty(cls)) {
            for (Class class1 : cls) {
                addDict(class1);
            }
        }
    }

    private void addDict(Class<? extends Enum> clz) {
        Map map1 = EnumUtil.getEnumMap(clz);
        DictDto sdt = new DictDto();
        String dictType = AnnotationUtil.getAnnotationValue(clz, EnumDictType.class, "dictType");
        String dictName = AnnotationUtil.getAnnotationValue(clz, EnumDictType.class, "dictName");
        sdt.setDictType(dictType);
        sdt.setDictName(dictName);
        Set<String> set = map1.keySet();
        Long sort = 1L;
        List<DictDataDto> dictDataList = new ArrayList<DictDataDto>();
        DictDataDto ddd = null;
        for (String key : set) {
            try {
                ddd = new DictDataDto();
                Object obj = map1.get(key);
                String code = ReflectUtil.invoke(obj, "getCode");
                String name = ReflectUtil.invoke(obj, "getName");
				/*String genDict="1";
				try {
					genDict=ReflectUtil.invoke(obj, "getGenDict");
				} catch (Exception e) {
					genDict="1";
				}*/
                Field field = ReflectUtil.getField(obj.getClass(), "genDict");
                if (Objects.nonNull(field)) {
                    String genDict = ReflectUtil.invoke(obj, "getGenDict");
                    ddd.setGenDict(genDict);
                } else {
                    ddd.setGenDict("1");
                }
                ddd.setDictValue(code);
                ddd.setDictLabel(name);
                dictDataList.add(ddd);
            } catch (UtilException e) {
                e.printStackTrace();
            }
        }
        sdt.setDictDataList(dictDataList);
        add(sdt);
    }
    
    
    
    @Transactional(rollbackFor = Exception.class)
    public void add(DictDto dictDto) {

        SysDictType sdt=new SysDictType();
        sdt.setCreateBy("enum");
        sdt.setCreateTime(new Date());
        sdt.setDictType(dictDto.getDictType());
        sdt.setDictName(dictDto.getDictName());
        sdt.setStatus("0");
        sdt.setDictId(SnowflakeIdWorker.getUUID());
        dictTypeService.deleteByDictType(dictDto.getDictType());
        dictDataService.deleteByDictType(dictDto.getDictType());
        SysDictData sdd=null;
        if(StringUtils.isNotEmpty(dictDto.getDictDataList())){
            dictTypeService.insertDictType(sdt);
            Long sort=0L;
            for (DictDataDto ddd :dictDto.getDictDataList() ) {
                sdd=new SysDictData();
                sdd.setCreateBy("系统初始化");
                sdd.setCreateTime(new Date());
                sdd.setDictValue(ddd.getDictValue());
                sdd.setDictLabel(ddd.getDictLabel());
                sdd.setDictSort(sort);
                sort++;
                sdd.setDictType(sdt.getDictType());
                sdd.setStatus("0");
                sdd.setIsDefault("N");
                if("1".equals(ddd.getGenDict())) {
                    dictDataService.insertDictData(sdd);
                }
            }
        }
    }
}
